package com.pzl.vca;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity extends AppCompatActivity implements NetReceiver.INet {
    final String TAG = "jinx_videpchara";

    //用来播放渲染对方的视频数据
    SurfaceView surfaceViewRemote;
    //用来播放渲染自己摄像头的视频数据
    LocalSurfaceView localSurfaceView;
    //视频数据解码器，解码对方发送过来的视频数据
    PlayLiveDecodec playLiveDecodec;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 100);

        Intent data = getIntent();
        String ip = data.getStringExtra("ip");
        String pushPort = data.getStringExtra("pushPort");
        boolean remoteSurfaceInitDelay = data.getBooleanExtra("remoteSurfaceInitDelay", false);

        localSurfaceView = findViewById(R.id.surfaceLocal);
        Intent config = new Intent();
        config.putExtra("width", 1080);
        config.putExtra("height", 1920);
        config.putExtra("port", Integer.parseInt(pushPort));
        localSurfaceView.setConfig(config);

        long delayTime = remoteSurfaceInitDelay == true ? 3000 : 0;
        surfaceViewRemote = findViewById(R.id.surface_remote);
        surfaceViewRemote.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
                Log.i(TAG, "surfaceCreated , delayTime = " + delayTime);

                surfaceViewRemote.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startReceiveDecodingRender(ip);
                    }
                }, delayTime);

            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
            }
        });


    }


    @Override
    public void onReceveData(byte[] data) {
        //Log.i(TAG, "onReceveData data.len = " + data.length);

        //解码对方发送过来的每一帧的压缩编码好的图像数据
        playLiveDecodec.decodingRender(data);
    }

    //启动接收视频数据并解码渲染
    private void startReceiveDecodingRender(String ip) {
        //启动连接对方视频数据的推流服务端
        NetReceiver netReceiver = new NetReceiver(MainActivity.this, ip);
        netReceiver.start();

        //配置初始化视频数据解码器
        playLiveDecodec = PlayLiveDecodec.getInstance();
        playLiveDecodec.initDecoder(surfaceViewRemote.getHolder().getSurface(), 1080, 1920);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}